package com.hlingsoft.kkh.action.admin;

import com.hlingsoft.kkh.constant.JobConstant;
import com.hlingsoft.kkh.dao.AdminCategoryDao;
import com.hlingsoft.kkh.dao.AdminJobDao;
import com.hlingsoft.kkh.entity.Job;
import com.hlingsoft.kkh.entity.vo.JsonVo;
import com.hlingsoft.kkh.entity.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by selbylei on 2015/1/15.
 */
@Controller
@RequestMapping("/admin/job")

public class AdminJobAction extends AdminBaseAction {
    @Autowired
    private AdminJobDao jobDao;
    @Autowired
    private AdminCategoryDao categoryDao;



    @RequestMapping(value = "/page.htm", method = RequestMethod.GET)
    public String allList(
            @RequestParam(value = "p", defaultValue = "1") int pageNum,
            ModelMap modelMap) {
        PageVo<Job> pageVo = new PageVo<Job>(pageNum);
        pageVo.setRows(10);
        pageVo.basePath="admin/job/page.htm" ;

        pageVo.setList(jobDao.getAllJobList(pageVo.getOffset(), pageVo.getRows()));
        pageVo.setCount(jobDao.getAllJobListCount());
        modelMap.put("categoryList",categoryDao.getCategoryList());
        modelMap.put("pageVo", pageVo);
        modelMap.put("jobName", "");
        modelMap.put("jobAddress","");
        return "system/job/all";
    }

    @RequestMapping(value = "/toSearch.htm")
    public String toSearchList(@RequestParam(value = "jobName")String jobName,
                               @RequestParam(value = "jobAddress") String jobAddress,
                               @RequestParam(value = "jobType",required = false) String jobType,
                               @RequestParam(value = "p", defaultValue = "1") int pageNum,
                               HttpServletRequest request,
                               ModelMap modelMap) {

        if(request.getMethod().equals("GET"))
        {
            jobName = encodeStr(jobName);
            jobAddress=encodeStr(jobAddress);
        }

        PageVo<Job> pageVo = new PageVo<Job>(pageNum);
        pageVo.setRows(10);
        Map<String, String> args =new HashMap<String, String>()   ;
        args.put("jobName",jobName);
        args.put("jobAddress",jobAddress);
        args.put("jobType",jobType);
        pageVo.setArgs(args);
        pageVo.basePath="admin/job/toSearch.htm" ;
        pageVo.setList(jobDao.getSearchJob(jobType,jobName,jobAddress,pageVo.getOffset(),pageVo.getRows()));
        pageVo.setCount(jobDao.getSearchJobCount(jobType,jobName, jobAddress));

        modelMap.put("pageVo", pageVo);
        modelMap.put("jobName", jobName);
        modelMap.put("jobAddress",jobAddress);
        modelMap.put("jobType",jobType);
        modelMap.put("categoryList",categoryDao.getCategoryList());
        return "system/job/all";
    }

    public String encodeStr(String str) {
        try {
            return new String(str.getBytes("ISO-8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping(value = "/toAdd.htm", method = RequestMethod.GET)
    public String toAdd(HttpServletRequest request, ModelMap modelMap) {
        modelMap.put("kindId", System.currentTimeMillis());
        modelMap.put("JSESSIONID", request.getSession().getId());
        modelMap.put("categoryList",categoryDao.getCategoryList());
        return "system/job/add";
    }

    @RequestMapping(value = "/delete.json")
    @ResponseBody
    public JsonVo<String> delete(@RequestParam(value = "jobId") long jobId,
                                 ModelMap modelMap) {
        JsonVo<String> json = new JsonVo<String>();
        jobDao.deleteJob(jobId);
        json.setResult(true);
        return json;
    }

    @RequestMapping(value = "/detail.htm",method = RequestMethod.GET)
    public String detail(@RequestParam(value = "jobId") long jobId,
                         HttpServletRequest request, ModelMap modelMap) {
        modelMap.put("kindId", System.currentTimeMillis());
        modelMap.put("JSESSIONID", request.getSession().getId());
        modelMap.put("job", jobDao.getJobById(jobId));
        modelMap.put("categoryList",categoryDao.getCategoryList());
        return "system/job/edit";
    }

    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");

    @RequestMapping(value = "/add.json", method = RequestMethod.POST)
    @ResponseBody
    public JsonVo<String> add(
            @RequestParam(value = "merchantId") long merchantId,
            @RequestParam(value = "name") String name,
            @RequestParam(value = "jobType") int jobType,
            @RequestParam(value = "jobAddress")String jobAddress,
            @RequestParam(value = "jobTime")String jobTime,
            @RequestParam(value = "recruitNum") String recruitNum,
            @RequestParam(value = "amount") String amount,
            @RequestParam(value = "startTime") String startTime,
            @RequestParam(value = "endTime") String endTime,
            @RequestParam(value = "payCycle") String payCycle,
//            @RequestParam(value = "ImageUrl") String imageUrl,
            @RequestParam(value = "description") String description,
            ModelMap modelMap) {
        JsonVo<String> json = new JsonVo<String>();
        try {

            json.setResult(true);
            Job job = new Job();
            job.setMerchantId(merchantId);
            job.setName(name);
            job.setJobType(jobType);
            job.setJobAddress(jobAddress);
            job.setJobTime(jobTime);
            job.setImageUrl("");
            job.setCreateTime(new Date());
            job.setRecruitNum(Integer.valueOf(recruitNum));
            job.setCandidateNum(0);
            job.setSalaryType(JobConstant.SalaryType.cash);
            job.setAmount(Integer.valueOf(amount));
            job.setUpdateTime(new Date());
            job.setStartTime(sf.parse(startTime));
            job.setEndTime(sf.parse(endTime));
            job.setStatus(JobConstant.Status.audit);
            job.setPayCycle(JobConstant.PayCycle.valueOf(payCycle));
            job.setDescription(description);
            jobDao.addJob(job);
            return json;
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg("输入参数有误");
            json.setResult(false);
            return json;
        }
    }


    @RequestMapping(value = "/edit.json", method = RequestMethod.POST)
    @ResponseBody
    public JsonVo<String> edit(
            @RequestParam(value = "jobId") long jobId,
            @RequestParam(value = "merchantId") long merchantId,
            @RequestParam(value = "name") String name,
            @RequestParam(value = "jobAddress")String jobAddress,
            @RequestParam(value = "jobTime")String jobTime,
////            @RequestParam(value = "imgUrl") String imgUrl,
            @RequestParam(value = "recruitNum") String recruitNum,
            @RequestParam(value = "candidateNum") String candidateNum,
            @RequestParam(value = "amount") String amount,
            @RequestParam(value = "startTime") String startTime,
            @RequestParam(value = "endTime") String endTime,
            @RequestParam(value = "status") String status,
            @RequestParam(value = "jobType") int jobType,
            @RequestParam(value = "payCycle") String payCycle,
            @RequestParam(value = "description") String description,

            ModelMap modelMap) {
        JsonVo<String> json = new JsonVo<String>();
        try {
            json.setResult(true);
            Job job = jobDao.getJobById(jobId);
            job.setMerchantId(merchantId);
            job.setName(name);
            job.setJobAddress(jobAddress);
            job.setJobTime(jobTime);
            job.setImageUrl("");
            job.setRecruitNum(Integer.valueOf(recruitNum));
            job.setCandidateNum(Integer.valueOf(candidateNum));
            job.setSalaryType(JobConstant.SalaryType.cash);
            job.setAmount(Integer.valueOf(amount));
            job.setUpdateTime(new Date());
            job.setStartTime(sf.parse(startTime));
            job.setEndTime(sf.parse(endTime));
            job.setStatus(JobConstant.Status.valueOf(status));
            job.setJobType(jobType);
            job.setPayCycle(JobConstant.PayCycle.valueOf(payCycle));
            job.setDescription(description);
            jobDao.updateJob(job);
            return json;
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg("输入参数有误");
            json.setResult(false);
            return json;
        }
    }
}
